<?php
namespace api\know\controller;

use cmf\controller\RestBaseController;
use api\know\model\ChapterModel;
use api\know\model\MaterialModel;
use think\facade\Log;

class ChapterController extends RestBaseController
{
    public function tree()
    {
        $grade = $this->request->param('grade', '');
        $subject = $this->request->param('subject', '');
		$version = $this->request->param('version', '');
        $volume = $this->request->param('volume', '');
		$material_type = $this->request->param('material_type', '');
        
        if (empty($grade))
		{
			$this->error('年级不能为空');
		} 
        else if (empty($subject))
		{
			$this->error('科目不能为空');
		}

        $w['material_type'] = 'teach';
        $w['is_delete'] = 0;
        $w['grade_code'] = $grade;
        $w['subject_code'] = $subject;
        $w['textbook_version_code'] = $version;
        $w['textbook_volume_code'] = $volume;
        $w['material_type'] = $material_type;
        
        $material = MaterialModel::where($w)->find();
        if (empty($material))
		{
			$this->error('未设置相关教材');
		}
        $model = new ChapterModel();
		$tree = $model->getChapterTree($material['uuid']);
        Log::debug($model->getLastSql());
        $this->success('请求成功!', $tree);
    }

    public function detail()
    {
        $uuid = $this->request->param('uuid', '');
		if (empty($uuid))
        {
            $this->error('id不能为空!');
        }

        $c_wh[] = ['is_delete', '=', 0];
        $c_wh[] = ['is_enable', '=', 1];
        $c_wh[] = ['uuid', '=', $uuid];

        $c = ChapterModel::where($c_wh)->field(ChapterModel::$QueryField)->find();
		if (empty($c)) 
		{
			$this->error('章节不存在!');
		}

        $this->success('请求成功!', $c);
    }
}
